/*
 * @Author: 肖芬芳 1911683843@qq.com
 * @Date: 2022-09-23 16:18:32
 * @LastEditors: 肖芬芳 1911683843@qq.com
 * @LastEditTime: 2022-12-09 15:34:08
 * @FilePath: \node_study\routes\admin\system\user\list.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const { json } = require('express');
const { object } = require('joi');
const {
    User
} = require('../../../../model/user.js');

module.exports = async (req, res) => {
    // 获取post参数
    // console.log(req.fields);
    let { pageSize, currentPage, ...user } = req.fields
    // console.log('user----------'+JSON.stringify(user))
    let users = []
    if (JSON.stringify(user) == '{}') {
        // 查询所有用户，逆序展示
        users = await User.find().select("-password").skip((currentPage - 1) * pageSize).limit(pageSize).sort('-createTime');
    } else {
        //模糊查询，生成正则表达式new RegExp(user.username)
        users = await User.find({
            $and: [{ 'username': new RegExp(user.username) }
                , { 'role': user.role == undefined ? new RegExp() : user.role }
                , { 'phone': new RegExp(user.phone) }
                , { 'status': user.status == undefined ? { $in: [1, 0] } : user.status }]
        }).select("-password").sort('-createTime').skip((currentPage - 1) * pageSize).limit(pageSize);
    }
    let totalCount = users.length;
    res.send({
        'code': 0,
        "totalCount": totalCount,
        "data": users
    });
}